"use strict"

const _ = require('lodash/fp')

module.exports = {
    normal : (live) => {
        const requests = []
        const iter = live.iterator()
        for(let i = 0; i < 10; i++) {
            requests.push(module.exports.await_for(iter))
        }

        return Promise.all(requests).then(_.flattenDeep).then(_.uniqBy('id'))
    },

    await_for : iterator => {

        let solve = []

        const _loop = (resolve, reject) => {
            const {value : next, done} = iterator.next()

            const exec_queue = result => {
                solve.push(result)
                _loop(resolve, reject)
            }

            if (done) return resolve(solve)
            
            next.then(exec_queue)
            .catch(err => {
                console.error(err)
                _loop(resolve, reject)
            })
        }
        
        return new Promise(_loop)
    }
}